﻿using System.IO;

namespace Pathfinder.Utilities
{
    public static class StreamReaderExtensions
    {
        public static bool ReadUntilString(this StreamReader self, string searchString)
        {
            char[] charArray = searchString.ToCharArray();
            char currentChar = char.MinValue;
            while (!self.EndOfStream)
            {
                while (currentChar != charArray[0] && !self.EndOfStream)
                {
                    char[] buffer = new char[1];
                    self.Read(buffer, 0, 1);
                    currentChar = buffer[0];
                }
                if (!self.EndOfStream)
                {
                    char[] buffer = new char[1];
                    int currentCharNumber = 1;
                    bool endLoop = false;
                    while (!endLoop && !self.EndOfStream)
                    {
                        self.Read(buffer, 0, 1);
                        currentChar = buffer[0];
                        if (currentChar == charArray[currentCharNumber])
                        {
                            currentCharNumber++;
                            if (currentCharNumber == searchString.Length)
                            {
                                return true;
                            }
                        }
                        else
                        {
                            endLoop = true;
                        }
                    }
                }
            }

            return false;
        }

        public static string StripHTML(this StreamReader self, string regexExpression)
        {
            string returnString = "";
            while (!self.EndOfStream)
            {
                returnString += self.ReadLine();
                returnString = returnString.StripHTML(regexExpression);
            }

            return returnString;
        }
    }
}
